package com.heima.user.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.heima.apis.admin.IAdminAuthNewsClient;
import com.heima.model.common.dtos.ResponseResult;
import com.heima.model.common.enums.AppHttpCodeEnum;
import com.heima.model.user.dtos.AuthDto;
import com.heima.model.user.pojos.ApUser;
import com.heima.model.user.pojos.ApUserRealname;
import com.heima.model.wemedia.pojos.WmUser;
import com.heima.user.mapper.ApUserMapper;
import com.heima.user.mapper.ApUserRealnameMapper;
import com.heima.user.service.ApUserRealnameService;
import com.heima.utils.thread.AdminThreadLocalUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;

/**
 * <p>
 * APP实名认证信息表 服务实现类
 * </p>
 *
 * @author itheima
 */
@Slf4j
@Service
@Transactional
public class ApUserRealnameServiceImpl extends ServiceImpl<ApUserRealnameMapper, ApUserRealname> implements ApUserRealnameService {
    /**
     * 查询认证列表
     *
     * @param authDto
     * @return
     */
    @Override
    public ResponseResult getList(AuthDto authDto) {
        //判断参数是否为空
        if (authDto==null){
            return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_INVALID);
        }
        //
        if (authDto.getPage() ==null && authDto.getSize()==null){
            return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_INVALID);
        }
        //分页查询
        if (authDto.getSize() ==null && authDto.getPage()==null){
            return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_INVALID);
        }
        IPage<ApUserRealname> page = new Page<>(authDto.getPage(),authDto.getSize());
        //查询条件
        LambdaQueryWrapper<ApUserRealname> queryWrapper = new LambdaQueryWrapper<>();
        //判断失败条件
        if (StringUtils.isNotBlank(authDto.getMsg())){
            queryWrapper.eq(ApUserRealname::getReason,authDto.getMsg());
        }
        //判断状态
        if (authDto.getStatus()!=null){
            queryWrapper.eq(ApUserRealname::getStatus,authDto.getStatus());
        }
        //分页条件查询
        page(page,queryWrapper);
        return ResponseResult.okResult(page);
    }

    /**
     * 审核失败,修改状态
     *
     * @param dto
     * @return
     */
    @Override
    public ResponseResult authFail(AuthDto dto) {
        //判断参数是否为空
        if (dto==null){
            return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_INVALID);
        }
        //判断是否有该用户认证信息
        if (dto.getId() ==null){
            return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_INVALID);
        }
        //查询认证信息是否存在
        ApUserRealname apUserRealname = getById(dto.getId());
        if (apUserRealname==null){
            return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_INVALID);
        }
        //判断状态是否存在
        if (dto.getStatus()==null){
            return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_INVALID);
        }
        //修改认证状态
        apUserRealname.setStatus(dto.getStatus());
        //修改失败原因
        if (StringUtils.isBlank(dto.getMsg())){
            return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_INVALID);
        }
        apUserRealname.setReason(dto.getMsg());
        //修改数据库
        updateById(apUserRealname);
        //修改后台用户认证状态
        ApUser apUser = new ApUser();
        apUser.setId(apUserRealname.getUserId());
        //认证失败状态
        apUser.setIdentityAuthentication(Boolean.FALSE);
        return ResponseResult.okResult(dto.getMsg());
    }

    @Autowired
    private IAdminAuthNewsClient userClient;

    @Autowired
    private ApUserMapper apUserMapper;
    /**
     * 审核成功,修改状态
     *
     * @param dto
     * @return
     */
    @Override
    public ResponseResult authPass(AuthDto dto) {
        //判断参数是否为空
        if (dto==null){
            return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_INVALID);
        }
        //判断是否有该用户认证信息
        if (dto.getId() ==null){
            return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_INVALID);
        }
        //判断状态是否存在
        if (dto.getStatus()==null){
            return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_INVALID);
        }
        //查询认证信息是否存在
        ApUserRealname apUserRealname = getById(dto.getId());
        if (apUserRealname==null){
            return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_INVALID);
        }
        //修改认证状态为成功
        apUserRealname.setStatus(dto.getStatus());
        //修改数据库
        updateById(apUserRealname);
        //根据用户id查询后台账号
        ApUser apUser = AdminThreadLocalUtil.getUser();
        if (apUser==null){
            return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_INVALID);
        }
        //查询apUser
        ApUser user = apUserMapper.selectById(apUser.getId());
        //修改apUser状态
        user.setIdentityAuthentication(Boolean.TRUE);
        //修改flag
        user.setFlag((short)1);
        apUserMapper.updateById(user);
        //通过审核开启自媒体账号
        WmUser wmUser = new WmUser();
        //复制值
        BeanUtils.copyProperties(user,wmUser,"id");
        //设置属性
        wmUser.setApUserId(user.getId());
        wmUser.setCreatedTime(new Date());
        ResponseResult responseResult = userClient.creatUser(wmUser);
        return responseResult;
    }
}
